'''
Created on Nov 29, 2010

@author: dexen
'''


import random

from dexen import BaseIndividual

from const import GEN_SIZE

ALIVE = "Alive"
DEAD = "Dead"

class Individual(BaseIndividual):
    def __init__(self):
        BaseIndividual.__init__(self)
        self.genotype = []
        for _ in range(GEN_SIZE):
            self.genotype.append(random.random())
        self.phenotype = None
        self.evalA = None
        self.evalB = None
        self.state = ALIVE

    def reproduce(self, ind):
        child1 = Individual()
        child2 = Individual()
        child1.genotype = []
        child2.genotype = []
        
        z = random.randint(1, GEN_SIZE-2)
        
        for i in range(z):
            child1.genotype.append(self.genotype[i])
            child2.genotype.append(ind.genotype[i])
            
        for i in range(z, GEN_SIZE):
            child1.genotype.append(ind.genotype[i])
            child2.genotype.append(self.genotype[i])
        
        return child1, child2
    
    def kill(self):
        self.state = DEAD
    
    def is_alive(self):
        return self.state == ALIVE 
    
    def __repr__(self):
        out = "---------- BaseIndividual ----------\n"
        out += "Id: %s\n"%(self._get_id()) 
        if self.genotype:
            out += "Genotype: Exists\n"
        else:
            out += "Genotype: None\n"
        if self.phenotype:
            out += "Phenotype: Exists\n"
        else:
            out += "Phenotype: None\n"        
        out += "EvalA: %s\n"%(self.evalA)
        out += "EvalB: %s\n"%(self.evalB)
        out += "-------------------------------\n"
        return out


#===============================================================================
# Testing
#===============================================================================
def test():
    ind = Individual()
    #print ind
    t = range(10)
    random.shuffle(t)
    print t
    
    
if __name__ == "__main__":
    test()
    